package com.movisol.tools.solvers;

import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import com.movisol.questionwizard.entities.ChoiceQuestion;
import com.movisol.questionwizard.entities.Question;

public class GenericMatrixSolver implements GenericSolverInterface {
	
	List<Question> questionList = null;
	int numberOfAnswers = 0;
	
	public GenericMatrixSolver(List<Question> list)
	{
		questionList = list;
	}

	public int calculateResult(int numberOfAnswers)
	{
		
		this.numberOfAnswers = numberOfAnswers;
		return calculateResult();
	}
	
	@Override
	public Integer calculateResult() {
		
		Iterator<Question> it = questionList.iterator();
		String resultValue;
		int[] array = new int[numberOfAnswers];
		
		while (it.hasNext()) 
		{
			resultValue = null;
			ChoiceQuestion actualQuestion = (ChoiceQuestion) it.next();
			resultValue = actualQuestion.getSelectedValue().getValue();
			
			StringTokenizer st = new StringTokenizer(resultValue, ",");
			int i = 0;
			while(st.hasMoreTokens())
			{
				array[i] += Integer.parseInt(st.nextToken());
				i++;
			}
		}
		int max = array[0];
		int pos = 0;
		for(int i = 1; i < array.length; i++)
		{
			if(array[i] > max)
			{
				max = array[i];
				pos = i;
			}
		}
		
		
		return pos+1;

	}

}
